package weixin.popular.api;

import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.entity.StringEntity;

import weixin.popular.bean.AddTemplateResponse;
import weixin.popular.bean.BaseResult;
import weixin.popular.bean.templatemessage.TemplateMessage;
import weixin.popular.bean.templatemessage.TemplateMessageResult;
import weixin.popular.client.LocalHttpClient;
import weixin.popular.util.JsonUtil;

import com.alibaba.fastjson.JSON;

public class TemplateAPI extends BaseAPI {

	public static BaseResult setIndustry(String access_token, String ndustryId1, String ndustryId2) {
		Map<String, String> param = new HashMap<String, String>();
		param.put("industry_id1", ndustryId1);
		param.put("industry_id2", ndustryId2);
		HttpUriRequest httpUriRequest = RequestBuilder.post()
				.setHeader(jsonHeader)
				.setUri("https://api.weixin.qq.com/cgi-bin/template/api_set_industry")
				.addParameter("access_token", access_token)
				.setEntity(new StringEntity(JSON.toJSONString(param), "utf-8"))
				.build();
		return LocalHttpClient.executeJsonResult(httpUriRequest,BaseResult.class);
	}
	
	public static AddTemplateResponse addTemplate(String access_token, String template_id_short) {
		Map<String, String> param = new HashMap<String, String>();
		param.put("template_id_short", template_id_short);
		HttpUriRequest httpUriRequest = RequestBuilder.post()
				.setHeader(jsonHeader)
				.setUri("https://api.weixin.qq.com/cgi-bin/template/api_add_template")
				.addParameter("access_token", access_token)
				.setEntity(new StringEntity(JSON.toJSONString(param), "utf-8"))
				.build();
		return LocalHttpClient.executeJsonResult(httpUriRequest,AddTemplateResponse.class);
	}
	
	/**
	 * 模板消息发送
	 * @param access_token
	 * @param templateMessage
	 * @return
	 */
	public static TemplateMessageResult messageTemplateSend(String access_token,TemplateMessage templateMessage){
		String messageJson = JsonUtil.toJSONString(templateMessage);
		HttpUriRequest httpUriRequest = RequestBuilder.post()
				.setHeader(jsonHeader)
				.setUri(BASE_URI+"/cgi-bin/message/template/send")
				.addParameter("access_token", access_token)
				.setEntity(new StringEntity(messageJson,Charset.forName("utf-8")))
				.build();
		return LocalHttpClient.executeJsonResult(httpUriRequest,TemplateMessageResult.class);
	}
	
}
